package org.mybatis.generator.my.config;

import org.mybatis.generator.my.exception.NotInitException;

import java.io.InputStream;
import java.util.Properties;

public class PropertiesConfig implements IConfig {

    private static final String JDBC_DRIVER_CLASS_KEY = "jdbc.driverClass";
    private static final String JDBC_CONNECTION_URL_KEY = "jdbc.connectionUrl";
    private static final String JDBC_USER_ID_KEY = "jdbc.userId";
    private static final String JDBC_PASSWORD_KEY = "jdbc.password";
    private static final String GEN_DIALECT_KEY = "generator.dialect";
    private static final String GEN_IS_OVERWRITE_KEY = "generator.overwrite";
    private static final String GEN_BASE_PACKAGE_KEY = "generator.basePackage";
    private static final String GEN_VIEW_PACKAGE_KEY = "generator.viewPackage";
    private static final String GEN_OUTPUT_PATH_KEY = "generator.outputPath";
    private static final String GEN_MODEL_PACKAGE_KEY = "generator.modelPackage";
    private static final String GEN_MAPPER_PACKAGE_KEY = "generator.mapperPackage";
    private static final String GEN_MAPPER_POSITION_KEY = "generator.mapperPosition";
    private static final String GEN_IS_SERIALIZABLE_KEY = "generator.isSerializable";
    private static final String GEN_AUTHOR_KEY = "generator.author";
    private static final String GEN_FREEMARKER_ENCODING_KEY = "generator.freemarker.encoding";
    private static final String GEN_CONTROLLER_SUFFIX_KEY = "generator.suffix.controller";
    private static final String GEN_VIEW_SUFFIX_KEY = "generator.suffix.view";
    private static final String GEN_MAPPER_SUFFIX_KEY = "generator.suffix.mapper";

    /**
     * properties file name
     */
    private String propertiesFileName;
    /**
     * freemarker template file position
     */
    private String templatePath;
    /**
     * properties
     */
    private Properties properties;

    public PropertiesConfig(String propertiesFileName) {
        this.propertiesFileName = propertiesFileName;
    }

    @Override
    public void init() throws Exception {
        this.templatePath = System.getProperty("user.dir") + "/src/main/resources/template";
        this.properties = new Properties();
        InputStream propInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(this.propertiesFileName);
        properties.load(propInputStream);
    }

    @Override
    public String getConnectionUrl() {
        checkProperties();
        return properties.getProperty(JDBC_CONNECTION_URL_KEY);
    }

    @Override
    public String getDriverClass() {
        checkProperties();
        return properties.getProperty(JDBC_DRIVER_CLASS_KEY);
    }

    @Override
    public String getUserId() {
        checkProperties();
        return properties.getProperty(JDBC_USER_ID_KEY);
    }

    @Override
    public String getPassword() {
        checkProperties();
        return properties.getProperty(JDBC_PASSWORD_KEY);
    }

    @Override
    public Boolean isOverwrite() {
        checkProperties();
        return Boolean.parseBoolean(properties.getProperty(GEN_IS_OVERWRITE_KEY));
    }

    @Override
    public String getOutputPath() {
        checkProperties();
        return properties.getProperty(GEN_OUTPUT_PATH_KEY);
    }

    @Override
    public String getBasePackage() {
        checkProperties();
        return properties.getProperty(GEN_BASE_PACKAGE_KEY);
    }

    @Override
    public Boolean isSerializable() {
        checkProperties();
        return Boolean.parseBoolean(properties.getProperty(GEN_IS_SERIALIZABLE_KEY));
    }

    @Override
    public String getAuthor() {
        checkProperties();
        return properties.getProperty(GEN_AUTHOR_KEY);
    }

    @Override
    public String getServicePackage() {
        return this.getBasePackage() + ".service";
    }

    @Override
    public String getServiceImplPackage() {
        return this.getBasePackage() + ".service.impl";
    }

    @Override
    public String getControllerPackage() {
        return this.getBasePackage() + ".controller";
    }

    @Override
    public String getModelPackage() {
        checkProperties();
        return new StringBuilder(this.getBasePackage())
                .append(".")
                .append(properties.getProperty(GEN_MODEL_PACKAGE_KEY))
                .toString();
    }

    @Override
    public String getMapperPackage() {
        checkProperties();
        return new StringBuilder(this.getBasePackage())
                .append(".")
                .append(properties.getProperty(GEN_MAPPER_PACKAGE_KEY)).toString();
    }

    @Override
    public String getTemplatePath() {
        return this.templatePath;
    }

    @Override
    public String getViewPackage() {
        checkProperties();
        return new StringBuilder(this.getBasePackage())
                .append(".")
                .append(properties.getProperty(GEN_VIEW_PACKAGE_KEY)).toString();
    }

    @Override
    public String getDefaultEncoding() {
        checkProperties();
        return properties.getProperty(GEN_FREEMARKER_ENCODING_KEY);
    }

    @Override
    public String getControllerSuffix() {
        checkProperties();
        return properties.getProperty(GEN_CONTROLLER_SUFFIX_KEY);
    }

    @Override
    public String getViewSuffix() {
        checkProperties();
        return properties.getProperty(GEN_VIEW_SUFFIX_KEY);
    }

    @Override
    public String getMapperSuffix() {
        checkProperties();
        return properties.getProperty(GEN_MAPPER_SUFFIX_KEY);
    }

    @Override
    public String getMapperPosition() {
        checkProperties();
        return properties.getProperty(GEN_MAPPER_POSITION_KEY);
    }

    @Override
    public String getDialect() {
        checkProperties();
        return properties.getProperty(GEN_DIALECT_KEY);
    }

    private void checkProperties() {
        if (properties == null) {
            throw new NotInitException("config is not init.");
        }
    }
}
